import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;



public class Window extends JFrame{

	public Window() {
		server = new Connection();
		server.send("HELLO");
		this.setSize(800, 400);
		
		this.setLayout(new BorderLayout());
		
		panel = new JPanel();
		toUser = new JLabel("Do kogo : ");
		toGroup = new JLabel("Do jakiej grupy : ");
		receivers =new DefaultListModel();
		dodajOdbiorcow();
		lista = new JList(receivers);
		lista.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				
				wybrane();
				
			}
		});
		scrollPane = new JScrollPane(lista);
		receiversGroup =new DefaultListModel();
		dodajGrupy();
		listaGrup = new JList(receiversGroup);
		listaGrup.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				wybrane();
				
			}
		});
		
		
		
		scrollPaneGrup = new JScrollPane(listaGrup);
		panel.setLayout(new GridLayout(4, 1));
		panel.add(toUser);
		panel.add(scrollPane);
		panel.add(toGroup);
		panel.add(scrollPaneGrup);
		
		
		panel2 = new JPanel();
		toWho = new JLabel("To : ");
		JTFRec = new JTextField(20);
		panel2.add(toWho);
		panel2.add(JTFRec);
		
		buttonPanel = new JPanel();
		JBWyslij = new JButton("Wyslij");
		JBOdbierz = new JButton("Odbierz");
		JBNowa = new JButton("Nowa Grupa");
		JBLoguj = new JButton("Zaloguj");
		JBNewUser = new JButton("Zarejestruj");
		JBUsun= new JButton("Usun swoja wiadomosc");
		
		buttonPanel.add(JBLoguj);
		buttonPanel.add(JBNewUser);
		buttonPanel.add(JBWyslij);
		buttonPanel.add(JBOdbierz);
		buttonPanel.add(JBNowa);
		buttonPanel.add(JBUsun);
		
		JBNowa.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String newGroup;
				newGroup = JOptionPane.showInputDialog("Podaj nazwa nowej Grupy");
				
				String[] tab=newGroup.split(",");
				
				String[] res;
				if(tab[1].equals("public"))res=server.send("SETGROUP "+tab[0]+" "+"0");
				else res=server.send("SETGROUP "+tab[0]+" "+"1");
				
				if(res[1].equals("NOUSER"))JOptionPane.showMessageDialog(null, "Nie jestes zalogowany");
				else{
				
				if(res[1].equals("GREXISTS"))JOptionPane.showMessageDialog(null, "Grupa o podanej nazwie juz istnieje.");
				else
				{
				receiversGroup.addElement(tab[0]);
				
				panel.invalidate();
				panel.validate();
				}
				}
			}
		});
		
		
		JBOdbierz.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
			
				int licz=0;
				String[] res=server.send("LISTPRIV");
				if(res[1].equals("NOUSER"))JOptionPane.showMessageDialog(null, "Nie jestes zalogowany");
				else{
				int ile = Integer.parseInt(res[1]);
				res=server.send("LISTUSRGROUPS");
				int ilegrup = Integer.parseInt(res[1]);
				if(ile==0)JOptionPane.showMessageDialog(null,"Nie masz wiadomosci");
				String msgs="";
				for(int i=0;i<ile;i++)
				{
				int q;
				
				res=server.send("GETPRIVMSG "+i);
				
				msgs+="prywatna "+licz+": "+res[0].substring(95)+" from "+res[0].substring(6, 37).trim();
				msgs+="\n";
				licz++;
				}
				
				for(int i=0;i<ilegrup;i++)
				{
					res=server.send("LISTGROUP "+i);
					int ilewiadomosci = Integer.parseInt(res[1]);
					for(int j=0;j<ilewiadomosci;j++)
					{
						res=server.send("GETGRMSG "+i+" "+j);
						
						msgs+="grupowa "+licz+": "+res[0].substring(95)+" from "+res[0].substring(32, 64).trim();
						msgs+="\n";	
						licz++;
					}
				}
				
				JTAOdebrane.setText(msgs);
				}
			}
		});
		
		JBUsun.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String[] res=server.send("LISTMYMSG");
				int ile = Integer.parseInt(res[1]);
				String msgs="";
				if(ile==0)JOptionPane.showMessageDialog(null,"Nie masz swoich wiadomosci");
				else
				{
					for(int i=0;i<ile;i++)
					{
						String[] res2=server.send("GETMYMSG "+i);
						msgs+=i+": "+res2[0].substring(95)+" to "+res2[0].substring(32, 64).trim()+"\n";
					}
					JTAOdebrane.setText(msgs);
					String ktora = JOptionPane.showInputDialog("Ktora wiadomosc chcesz usunac?");
					res=server.send("DELMYMSG "+ktora);
				}
			}
		});
		
		
		JBLoguj.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//server.send("czesc");
				
				logWindow = new LogWindow(server);
				JTAOdebrane.setText(" ");
				JTATresc.setText(" ");
				JTFRec.setText(" ");
				
				
			}
		});
		
		JBNewUser.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				regWindow = new RegWindow(server);
			}
		});
		
		
		panel3 = new JPanel();
		JTATresc = new JTextArea(10,40);
		
		JTAOdebrane = new JTextArea(10,40);
		panel3.add(JTATresc);
		panel3.add(JTAOdebrane);
		JBWyslij.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {

				String data= JOptionPane.showInputDialog(null,"Podaj date wygasniecia wiadomosci");
				data+=" ";
				String odbiorca=JTFRec.getText().trim();
				String nadawca = logWindow.getLogin();
				String tresc = JTATresc.getText();
				Integer size = tresc.length();
				
				String msg="";
				if(receiversGroup.contains(odbiorca))msg+="1";
				if(receivers.contains(odbiorca))msg+="0";
				msg+=odbiorca;
				for(int i=0;i<32-odbiorca.length();i++)msg+=" ";
				msg+=nadawca;
				for(int i=0;i<32-nadawca.length();i++)msg+=" ";
				msg+=tresc.length();
				for(int i=0;i<4-size.toString().length();i++)msg+=" ";
				msg+=data;
				msg+="0 ";
				msg+=tresc;
				String[] res=server.send("SENDMSG "+msg);
				if(res[1].equals("NOUSER"))JOptionPane.showMessageDialog(null, "Nie jestes zalogowany");
				else{
				if(!res[1].equals("MSGSENT"))JOptionPane.showMessageDialog(null,"Nie udalo sie wyslac wiadomosci" );
				JTATresc.setText(" ");
				}
				
			}
		});
		
		this.add(panel3,BorderLayout.CENTER);
		this.add(buttonPanel,BorderLayout.SOUTH);
		this.add(panel2,BorderLayout.NORTH);
		this.add(panel,BorderLayout.EAST);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	
	public void wybrane()
	{
		
		Object[] tab = lista.getSelectedValues();
		Object[] tab2 = listaGrup.getSelectedValues();
		sb.delete(0,sb.length());
		for(Object o:tab)
		{
			sb.append((String)o);
			sb.append(" ");
		}
		for(Object o:tab2)
		{
			sb.append((String)o);
			sb.append(" ");
		}
		
		JTFRec.setText(sb.toString());
	}
	
	public void dodajOdbiorcow()
	{
		
		receivers.addElement("wojtek");
		receivers.addElement("krzysiek");
		receivers.addElement("szymon");
		receivers.addElement("nowyUser");
		receivers.addElement("pryngiel");
		
	}
	
	public void dodajGrupy()
	{
		
		receiversGroup.addElement("GrupaPodstawowa");
		receiversGroup.addElement("GrupaWojtka");
		receiversGroup.addElement("Nowa");
		receiversGroup.addElement("grupaPryngla");
		
	}
	
	Set<String> set = new HashSet<String>();
	String rec="";
	DefaultListModel receivers;
	DefaultListModel receiversGroup;
	StringBuilder sb = new StringBuilder("");
	JList lista;
	JPanel panel;
	JPanel panel2;
	JPanel buttonPanel;
	JList listaGrup;
	JPanel panelGrup;
	JPanel panel3;
	JLabel toUser;
	JLabel toWho;
	JLabel toGroup;
	JButton JBWyslij;
	JButton JBOdbierz;
	JButton JBDodajGrupe;
	JButton JBLoguj;
	JButton JBNewUser;
	JButton JBNowa;
	JButton JBUsun;
	JScrollPane scrollPane;
	JScrollPane scrollPaneGrup;
	JTextField JTFRec;
	JTextField JTFTime;
	JTextArea JTATresc;
	JTextArea JTAOdebrane;
	JComboBox JCBUsers;
	LogWindow logWindow;
	RegWindow regWindow;
	Connection server;
	
	
	
	
	
	
	
	
}
